import RoleAPI from "@/api/system/role";

const adminEditOption: object = {
  size: "large",
  labelWidth: 100,
  submitBtn: true,
  emptyBtn: true,
  column: [
    {
      label: "头像",
      prop: "avatar",
      type: "upload",
      limit: 1,
      accept: "image/*",
      fileType: "img",
      listType: "picture-card",
      oss: "qiniu",
    },
    {
      label: "姓名",
      prop: "name",
      rules: [{ required: true, message: "请输入姓名", trigger: "blur" }],
    },
    {
      label: "手机号",
      prop: "mobile",
      rules: [
        { required: true, validator: validPhone, trigger: "blur", message: "请输入正确的手机号码" },
      ],
    },
    {
      label: "账号",
      prop: "username",
      rules: [{ required: true, message: "请输入姓名", trigger: "blur" }],
    },
    {
      label: "密码",
      prop: "password",
      type: "password",
    },
    {
      label: "角色",
      prop: "auth_group_id",
      type: "select",
      dicData: () => {
        return RoleAPI.getOptions();
      },
      rules: [{ required: true, message: "请选择角色", trigger: "blur" }],
    },

    {
      label: "状态",
      prop: "status",
      type: "switch",
      dicData: [
        { label: "禁用", value: 0 },
        { label: "启用", value: 1 },
      ],
    },
  ],
};

export default adminEditOption;

/**
 * 验证手机号码的合法性
 *
 * @param rule 验证规则对象，用于定义验证的规则和条件
 * @param value 待验证的手机号码字符串
 * @param callback 回调函数，用于在验证完成后返回验证结果
 */
function validPhone(rule: object, value: string, callback: (error?: Error) => void): void {
  if (!/^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(value)) {
    callback(new Error());
  } else {
    callback();
  }
}
